package com.sm.scorpio.ui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.scorpio.baselibrary.ScreenAdapterTools;
import com.sm.scorpio.R;
import com.sm.scorpio.ui.base.BaseDialog;


/**
 * 通用的确认取消弹窗
 * created by 包子 on 2018/12/15
 */
public class ContentDialog extends BaseDialog implements View.OnClickListener {
    private OnDialogClickListener dialogClickListener;
    private Options options;
    public static final int OK = 1;
    public static final int CLOSE = 0;

    public ContentDialog(@NonNull Context context) {
        super(context);
        v.addOnClickListener(this, R.id.dialog_affirm_btn, R.id.dialog_close_btn);
    }

    @Override
    protected int onCreateLayout() {
        return R.layout.dialog_affirm_content;
    }

    public ContentDialog setDialogTitle(int res_id) {
        return setDialogTitle(mContext.getString(res_id));
    }

    public ContentDialog setDialogTitle(String text) {
        v.setText(R.id.dialog_title_view, text);
        return this;
    }

    public ContentDialog setContent(int res_id) {
        return setContent(mContext.getString(res_id));
    }

    public ContentDialog setContent(String text) {
        v.setText(R.id.dialog_context_view, text);
        return this;
    }

    //显示关闭按钮
    public ContentDialog showCloseBtn() {
        v.setGone(R.id.dialog_close_btn, true);
        return this;
    }

    public void setDialogClickListener(OnDialogClickListener dialogClickListener) {
        this.dialogClickListener = dialogClickListener;
    }

    @Override
    public void show() {
        if (options != null) {
            if (options.title != null) {
                setDialogTitle(options.title);
            }
            v.setText(R.id.dialog_affirm_btn, TextUtils.isEmpty(options.confirmString) ? mContext.getString(R.string.string_00052) : options.confirmString)
                    .setText(R.id.dialog_close_btn, TextUtils.isEmpty(options.cancelString) ? mContext.getString(R.string.string_00053) : options.cancelString)
                    .setGone(R.id.dialog_title_view, options.isShowTitle)
                    .setTextColor(R.id.dialog_title_view, options.contentTextColor)
                    .setTextSize(R.id.dialog_title_view, ScreenAdapterTools.getInstance().loadCustomAttrValue(options.contentTextSize));
            setContent(options.content);
            if (options.isShowClose) showCloseBtn();
            if (options.dialogClickListener != null)
                setDialogClickListener(options.dialogClickListener);
        }
        super.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_affirm_btn:
                if (dialogClickListener != null) {
                    if (dialogClickListener.onDialogItemClickListener(OK)) {
                        dismiss();
                    }
                    return;
                }
                break;
            case R.id.dialog_close_btn:
                if (dialogClickListener != null) {
                    if (dialogClickListener.onDialogItemClickListener(CLOSE)) {
                        dismiss();
                    }
                    return;
                }
                break;
        }
        dismiss();
    }

    public interface OnDialogClickListener {
        boolean onDialogItemClickListener(int result);
    }

    public void setOptions(Options options) {
        this.options = options;
    }

    public static class Options {
        String title = null;
        String content = "";
        String confirmString = "";
        String cancelString = "";
        boolean isShowClose = false;
        boolean isShowTitle = false;
        OnDialogClickListener dialogClickListener;
        int contentTextSize = 43;
        int contentTextColor = Color.parseColor("#666666");

        //弹窗标题
        public Options setTitle(String title) {
            this.title = title;
            return this;
        }

        public Options setContentTextSize(int contentTextSize) {
            this.contentTextSize = contentTextSize;
            return this;
        }

        public Options setContentTextColor(int contentTextColor) {
            this.contentTextColor = contentTextColor;
            return this;
        }

        //设置确认按钮 文案
        public Options setConfirmString(String confirmString) {
            this.confirmString = confirmString;
            return this;
        }

        //取消按钮文案
        public Options setCancelString(String cancelString) {
            this.cancelString = cancelString;
            return this;
        }

        //显示内容
        public Options setContent(String content) {
            this.content = content;
            return this;
        }

        //显示关闭按钮
        public Options showCloseBtn() {
            isShowClose = true;
            return this;
        }

        //是否显示标题
        public Options showTitle(boolean isShowTitle) {
            this.isShowTitle = isShowTitle;
            return this;
        }

        //按钮回调
        public Options setDialogClickListener(OnDialogClickListener dialogClickListener) {
            this.dialogClickListener = dialogClickListener;
            return this;
        }
    }
}
